<template>
  <div>
       <!-- 弹出层的头部区域 -->
    <van-nav-bar title="频道管理">
      <template #right>
        <van-icon
        name="cross"
        size="14"
        color="white"
         @click="$emit('colseFN')" />
      </template>
    </van-nav-bar>
    <!-- 我的频道 -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span>我的频道:  <span style="font-size:10px">{{show ? '点击移除频道' :'点击进入频道'}}</span>  </span>
        <span @click="show=!show">{{show ? '完成':'编辑'}}</span>
      </div>
      <!-- 我的频道列表 -->
      <van-row type="flex">
        <van-col
          span="6"
          v-for="obj in userChannelsList" :key="obj.id"
           @click="goChannelFn(obj)"
        >
          <div class="channel-item van-hairline--surround">
            {{obj.name}}
            <!-- 删除的徽标 -->
            <van-badge
              color="transparent"
              class="cross-badge"
              v-if="show&&obj.id!==0"

            >
              <template #content>
                <van-icon
                  name="cross"
                  class="badge-icon"
                  color="#cfcfcf"
                  size="0.32rem"
                  @click="delFn (obj)"
                />
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道 -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <van-col
        span="6"
        v-for="obj in unChannelList" :key="obj.id"
        @click="addFn(obj)"
         >
          <div class="channel-item van-hairline--surround">
            {{obj.name}}
          </div>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
import { allChannelListAPI, updateChannelListAPI } from '@/api'
export default {
  data () {
    return {
      show: false, // 编辑×号显示状态
      allChannelList: [] // 所有频道列表
    }
  },
  props: {
    userChannelsList: {
      type: Array,
      required: true,
      default: () => {}
    }
  },
  async  created () {
    // 获取所有频道列表
    const res = await allChannelListAPI()
    this.allChannelList = res.data.data.channels
  },
  computed: {
    // 得到未选列表
    unChannelList () {
      return this.allChannelList.filter(obj => {
        const index = this.userChannelsList.findIndex(item => {
          return item.id === obj.id
        })
        if (index !== -1) { // 找到相同的了
          return false // filter方法 return false  不会收集这些obj
        } else {
          return true // filter方法 return true  会收集这些obj,且返回一个以后obj为内容的新的数组
        }
      })
    }
  },
  methods: {
    //   添加频道
    addFn (obj) {
      if (this.show === true) {
        this.userChannelsList.push(obj)
        this.undatedFn()
      }
    },
    // 删除频道
    delFn (obj) {
      if (obj.name !== '推荐') {
        const index = this.userChannelsList.findIndex(item => {
          return item.id === obj.id
        })
        this.userChannelsList.splice(index, 1)
        this.undatedFn()
      }
    },
    // 更新频道(发请求)
    async undatedFn () {
      // 二层深拷贝,拷贝到新数组,否则影响原列表
      const arr = []
      this.userChannelsList.forEach(obj => {
        const newObj = { ...obj }
        arr.push(newObj)
      })
      // 删除推荐对象
      const index = arr.findIndex(obj => {
        return obj.name === '推荐'
      })
      arr.splice(index, 1)
      // 删除对象内的name属性键值对,添加seq属性及键值对
      arr.forEach((obj, index) => {
        delete obj.name
        obj.seq = index + 1
      })
      // 发送请求
      const res = await updateChannelListAPI({ channels: arr })
      console.log(res)
    },
    // 点击关闭弹出层,进入频道进入
    goChannelFn (obj) {
      if (this.show === false) {
        this.$emit('colseFN')
        this.$emit('goChannelFn', obj)
      }
    }

  }
}
</script>

<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small_title {
  font-size: 12px;
  color: gray;
}
</style>
